package com.lbn.baseframe.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.lbn.baseframe.mvp.BasePresenter;
import com.lbn.baseframe.mvp.IMvp;
import com.lbn.baseframe.ui.IAppbar;
import com.lbn.baseframe.utils.EasyToast;
import com.lbn.baseframe.utils.LoadingUtils;
import com.lypeer.fcpermission.ui.FcPermissionsActivity;

import java.util.List;

/**
 * @author 李炳南
 * @date 2017/2/8
 */
public class BaseActivity <P extends BasePresenter> extends FcPermissionsActivity implements IBase,IMvp{

    protected P mPresenter;

    protected IAppbar appbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //最终方案
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏 为了适配有虚拟按键的手机，不要打开这项
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if(appbar!=null){
            appbar.attachView(this);
        }
        mPresenter = createPresenter();
        if(mPresenter!=null){
            mPresenter.attachView(this);
            onPresenterCreated();
        }
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onPostCreate(savedInstanceState, persistentState);
    }

    /**
     * @return
     * @desc 创建Presenter
     */
    public P createPresenter() {
        return null;
    }


    /**
     * @desc Presenter创建完成
     */
    public void onPresenterCreated(){}

    /**
     * @desc 不再保存Fragment的状态，达到fragment随MyActivity一起销毁
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.destroy();
        }
    }

    @Override
    public void setAppbar(IAppbar appbar) {
        this.appbar = appbar;
    }


    @NonNull
    @Override
    protected String getRationale4NeverAskAgain() {
        return null;
    }

    @Override
    public void onPermissionDenied(int requestCode, List<String> perms) {

    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {

    }

    @Override
    public void onError(String error) {
        EasyToast.showShort(this,error);
    }

    @Override
    public void showLoading() {
        LoadingUtils.showDialog(getSupportFragmentManager());
    }

    @Override
    public void hideLoading() {
        LoadingUtils.dismiss(getSupportFragmentManager());
    }
}
